"""
@name : python正则表达式
@author : wenyao
@projectname: xsfh
"""
#正则表达式  unix系统
#1、在一大堆文本字符串中找到自己想要的字符串
#2、验证输入是否合法   ip地址  ipv4 10.0.0.0

#正则表达式  不是python特有 有自己独立的引擎

#re  模块
#1、search和match查找
#search 和match都是查找第一个匹配。
#match 只能从字符串的开头查找，开始部分没有找到，那就不会匹配上。
# #match 只匹配字符串的开始
# >>> result = re.search("sanchuang","hello world, this is sanchuang")
# >>> result
# <_sre.SRE_Match object; span=(21, 30), match='sanchuang'>
# >>> result = re.search("sanchuang","hello world, this is")
# >>> result
# >>> result = re.search("sanchuang","hello world, this is sanchuang sanchuang")
# >>> result
# <_sre.SRE_Match object; span=(21, 30), match='sanchuang'>
# >>> result = re.match("sanchuang","hello world, sanchuang")
# >>> result
# >>> result = re.match("sanchuang","sanchuangsanchuang, hello world, sanchuang")
# >>> result
# <_sre.SRE_Match object; span=(0, 9), match='sanchuang'>


#match对象
# >>> result = re.search("world","hello world")
# >>> dir(result)
# ['__class__', '__copy__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__','__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string']
# >>> result.end
# <built-in method end of _sre.SRE_Match object at 0x7f5c0b9cc098>
# >>> result.end()
# 11
# >>> result.start()
# 6
# >>> result.group()
# 'world'
# >>> result.group
# <built-in method group of _sre.SRE_Match object at 0x7f5c0b9cc098>
# >>> result.group()
# 'world'
# >>>

#findall 和 finditer
# >>> msg = "I love pythonpython1 python2"
# >>> re.findall("python",msg)
# ['python', 'python', 'python']
# >>> re.finditer("python",msg)
# <callable_iterator object at 0x7f5c0b0b7ba8>
# >>> for i in re.finditer("python",msg):
# ...    print(i)
# ...
# <_sre.SRE_Match object; span=(7, 13), match='python'>
# <_sre.SRE_Match object; span=(13, 19), match='python'>
# <_sre.SRE_Match object; span=(21, 27), match='python'>
# >>> for i in re.finditer("python",msg):
# ...    print(i.group())
# ...
# python
# python
# python

#正则替换
# >>> msg = "I love python"
# >>> re.sub("py","PY",msg)
# 'I love PYthon'
# >>> result = re.sub("py","PY",msg)
# >>> result
# 'I love PYthon'
# >>> msg = "I love python python"
# >>> result = re.sub("py","PY",msg)
# >>> result
# 'I love PYthon PYthon'

#正则编译
# >>> reg = re.compile("py")
# >>> reg.findall(msg)
# ['py', 'py']

# re.findall("py",msg)


##################正则表达式##############
#区间匹配
import re
# ret = re.findall("[Pp]y","Python3 python Pthon")
# print(ret)
# ret = re.findall("[Pfg]ython", "Python python fython gython ython")
# print(ret)
# ret = re.findall("[0-9]","abc123")
# print(ret)
# ret = re.findall("[A-Z]","AbeDF12")
# print(ret)
# ret = re.findall("[A-z]","AbalsweWEF[")
# print(ret)
#字母数字下划线
#根据编码顺序
# ret = re.findall("[A-Za-z0-9_]","soe24r.esrg0g_")
# print(ret)

#区间取反
# ret = re.findall("[^A-Za-z0-9]","soe24r.esrg0g_")
# print(ret)

#或匹配
# msg = "xyf hzj lzw ly sh"
# print(re.findall("sh|hzj", msg))
# print(re.search("sh|hzj",msg))
#
# msg = "a3aab45c2"
# print(re.findall("[a-z][0-9]",msg))

#. 占位符  表示除换行符之外的任意一个字符
# ret = re.findall("p.thon", "Python python pgthon p thon pthon p\nthon")
# print(ret)

#快捷方式表示
#\A  匹配字符串的开始
#\b 词边界
#\w 匹配任何单词字符
#\W 匹配非单词字符
#\s  匹配空字符
#\S  匹配非空字符
#\d  匹配数字
#\D  匹配非数字
# ret = re.findall(r"\Aworld","world hello world")
# print(ret)

# ret = re.findall(r"\bworld\b", "world123 world# worldabc 123world")
# print(ret)
# ret = re.findall(r"\Bworld", "world123 world# worldabc 123world")
# print(ret)
# ret = re.findall(r"\w","2$%^dlx_")
# print(ret)
# ret = re.findall(r"\W","2$%^dlx_")
# print(ret)

#开始与结束
#匹配以python开头：^
#匹配以python结束： $
# ret = re.findall("^python","python hello python \npython")
# print(ret)
# ret = re.findall("python$","hello python")
# print(ret)

#通配符
#?  +   *
# #？  匹配0次到一次
# ret = re.findall("py?","py p pyython")
# print(ret)
# #+  匹配一次以上
# ret = re.findall("py+","py p pyython")
# print(ret)
#
# #* 匹配任意次 0-n次
# ret = re.findall("py*","py p pyython yy")
# print(ret)
#
# #{n,m} 指定匹配n到m次
# ret = re.findall("py{2,4}","py pyy pyyyy pyyyyyyy")
# print(ret)
#
# #贪婪匹配和非贪婪匹配
# msg = "<div>test</div>bb<div>test2</div>"
# ret = re.findall("<div>.*</div>",msg)
# print(ret)
# ret = re.findall("<div>.*?</div>",msg)
# print(ret)

######
#tel : 17375722991
#tel : 173-7572-2991
#tel : 173 7572 2991
#tel : (+86)17375722991

# msg = """
# #tel : 17375722991
# #tel : 173-7572-2991
# #tel : 173 7572 2991
# #tel : (+86)17375722991
# 17375722991123
# """

# ret = re.findall(r"\D(\(\+86\)\d{11})\D|\D(\d{3}[- ]?\d{4}[- ]?\d{4})\D", msg)
# print(ret)
#001  +001 11 +11
#0 +00 00

# msg="0 100 -100 +100 001 1.1 -1.1"
# 数值类型检测器
#按q退出
#输入一个数 判断输出是正整数，还是负整数，还是浮点数
# msg = input("请输入数字:")
# if re.findall(r"^\+?[1-9]\d*$|^\+?0+[1-9]\d*$",msg):
#    print(msg, "是正整数")

#正则分组0100

# def check():
#     if re.findall(r"^\+?[1-9]\d*$|^\+?0+[1-9]\d*$", msg):
#         print(msg, "是正整数")
#     elif re.findall(r"^\-[1-9]\d*$|^\-0+[1-9]\d*$", msg):
#         print(msg, "是负整数")
#     elif re.findall(r"^[+-]?\d+\.\d+$", msg):
#         print(msg, "是浮点数")
#     else:
#         print("您的输入不合法，请重新输入")
#
# print("进入检查数字系统，按exit随时退出")
# while True:
#     msg = input("请输入数字：")
#     if msg == "exit":
#         break
#     else:
#         check()
#

############正则分组#######
# ret = re.search(r"(\d{3})-(\d{3})-(\d){3}","abc123-465-789aaa")
# print(ret.group())
# print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))
# print(ret.group(3))

#捕获分组 (正则表达式)
#非捕获分组(?:正则表达式)

#msg = "hello sc1 hello sc1"
#msg = "hello sc1 hello2 sc2"
# msg = "aa bb aa bb"
#
# #分组向后引用
# #捕获分组（分组并捕获，分组之后匹配到的数据，会放在内存中，并且给定给一个从1开始索引）
# print(re.search(r"(\w+)\s(\w+)\s\1\s\2", msg).group())
#
# #使用findall 有捕获分组的话，只会显示捕获分组里的内容，
# print(re.findall(r"(\w+)\s(\w+)\s\1\s\2", msg))

#非捕获分组
#只分组不捕获  ，不会匹配到的项不保存在内存中，不会分配从1开始下标索引
# msg2 = "a1 a2 a1 a1"
# print(re.search(r"(?:\w+)\s(\w+)\s\1",msg2))
#
# print(re.search("\d+","abc123"))

##########
#findall如果有捕获分组，只会显示捕获分组里的内容
# msg2 = "a1 a2 a1 a1"
# print(re.findall(r"(?:\w+)\s(\w+)\s\1",msg2))

# msg = "comyyy@xx.comcom123@qq.comaaa@126.combbb@163.comcc@abc.com"
#--123  126  163邮箱捞出来
#com123@qq.com  aaa@126.com  bbb@163.com
#1、@后面只能接（123,126， 163）
# print(re.findall(r"(?:\.com)?(\w+@(?:qq|126|163).com)",msg))

#ipv4地址匹配
# 1-255.1-255.1-255.1-255
#ip地址：/^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$/
# print(re.search(r"^((2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(2[0-4][0-9]|25[0-5]|1[0-9][0-9]|[1-9][0-9]|[0-9])$","256.168.0.1"))

#邮箱地址匹配
#xx@xx.xx
#\w+@\w+\.\w+

#命名分组
# ret = re.search(r"(?P<first>\d{3})-(\d{3})-(\d){3}","abc123-465-789aaa")
# print(ret.group(1))
# print(ret.group("first"))
# print(ret.groupdict())

#正则标记
# msg = """
# PYTHON
# python
# """
# 对大小写不敏感的标志位
# ret = re.findall(r"python",msg,re.I)
# print(ret)

#不用多行模式 字符串就是一个整体匹配
#多行匹配模式  将字符串中的每一行内容一次做匹配
# ret = re.findall(r"^python$", msg, re.I|re.M)
# print(ret)
#
msg = """zhang shao han
zhang yi xing
zhang xue you
zhang jie
zhang fei
xie na
he jiong
zhang heng
"""
# ret = re.findall(r"^zhang\s[a-z]+$",msg, re.M)
# print(ret)
#
# #re.s
# ret = re.findall(r".+",msg, re.S)
# print(ret)


# 1、长度为8-10的用户密码（以字母、数字、下划线任意组合，以字母开头）
# ret = re.findall(r"^[a-zA-Z][A-Za-z0-9_]{7,9}$","a34sdf0345")
# print(ret)
#2、提取url .com或者.cn域名
# msg = """
# https://music.163.com/#discover/toplist
# http://www.baidu.com?a=2
# http://sc.cn
# https://new.xin.lang.cn
# http://ww...com
# """
# ret = re.findall(r"^https?://(?:[a-zA-Z0-9-]+\.)+(?:com|cn)",msg, re.M)
# print(ret)

#零宽断言
# s = "sc1 hello sc2 hello"
# #匹配 后面是 sc2的hello
# print(re.findall(r"hello(?= sc2)",s))
# #匹配 后面不是 sc2的hello
# print(re.findall(r"hello(?! sc2)",s))
# #匹配 前面是sc2 的hello
# print(re.findall(r"(?<=sc2 )hello",s))
# #匹配 前面不是sc2 的hello
# print(re.findall(r"(?<!sc2 )hello",s))

# msg = """
# 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
#     link/ether 00:0c:29:1b:68:1a brd ff:ff:ff:ff:ff:ff
#     inet 192.168.0.204/24 brd 192.168.0.255 scope global noprefixroute ens33
#        valid_lft forever preferred_lft forever
#     inet6 fe80::20c:29ff:fe1b:681a/64 scope link
#        valid_lft forever preferred_lft forever
# """
# print(re.findall(r"(?<=inet ).+(?=/)",msg))

msg = "inet 192.168.0.204/24 brd 192.168.0.255 scope global noprefixroute ens33"
print(re.findall(r"(?<=inet ).+(?=/)",msg))
#爬取图片
# 1、requests库的使用
# 2、使用requests请求三创官网
# 3、使用正则抓取图片地址， 如果图片地址是以/开头，那就将三创官网的地址和图片地址做个拼接
# 4、使用request请求图片地址，将获取到的图片保存在本地（open）

# #匹配里面的每一个单词，引号部分作为一个单词输出
# msg = "san chuang tong le, welcom ,'hello world', learn python"
